//
//  GameSetView.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/3/5.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "GameSetView.h"
#import "NSString+Other.h"

#define GAME_HOME_MUSIC @"GAME_HOME_MUSIC"

@interface GameSetView() {
    /// 默认 人数背景_右距离 默认: -20
    __weak IBOutlet NSLayoutConstraint *bgView_right_space;
}

@property (copy, nonatomic) ButtonBlock buttonBlock;

@property (weak, nonatomic) IBOutlet UILabel *personLabel;
@property (nonatomic, strong)IBOutlet UICollectionView *collectionView;
@end

@implementation GameSetView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    NSString *type =[NSString getUserDefault:GAME_HOME_MUSIC];
    self.musicButton.selected =[type boolValue];
    bgView_right_space.constant =-140;
    [self buttonAnimation:self.musicButton.selected];
}


- (void)buttonAnimation:(BOOL)isOpen {
    if (isOpen) {
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 4;
        rotationAnimation.cumulative = YES;
        rotationAnimation.removedOnCompletion = NO;
        rotationAnimation.fillMode = kCAFillModeForwards;
        rotationAnimation.repeatCount = HUGE_VALF;
        
        [self.musicButton.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    } else {
        [self.musicButton.layer removeAllAnimations];
    }
}

- (void)setWatchNumber:(NSString *)watchNumber {
    if (watchNumber && watchNumber.length>0) {
        _watchNumber =watchNumber;
        self.personLabel.text =[NSString stringWithFormat:@"%@\n围观", watchNumber];
    } else {
        _watchNumber =@"0";
        self.personLabel.text =@"0\n围观";
    }
}

- (IBAction)buttonClick:(UIButton *)sender {
    if (sender ==self.musicButton) {
        self.musicButton.selected =! self.musicButton.selected;
        [NSString insertUserDefault:@(self.musicButton.selected) key:GAME_HOME_MUSIC];
        [self buttonAnimation:self.musicButton.selected];
    } else if (sender ==self.numberBtn) {
        self.numberBtn.selected =! self.numberBtn.selected;
        if (self.numberBtn.selected) {
            bgView_right_space.constant =-20;
        } else {
            bgView_right_space.constant =-140;
        }
    }
    if (self.buttonBlock) {
        self.buttonBlock(sender);
    }
}

- (void)didButtonClickBlock:(ButtonBlock)newBlock {
    self.buttonBlock =newBlock;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
